#!/usr/bin/python2.4

# pylint: disable-msg=C6304

"""Tests the generation and scanning of simple windbarbs."""

import StringIO
import unittest

from matrix import BitMatrix
from matrix import Point
from matrix import Polar
from matrix import TransformStack
import windbarb


class WindBarbTest(unittest.TestCase):
  def GetExpectAsciiWindBarb(self):
    image_rows = [
        ' 24                                                   ',
        ' 23                                                   ',
        ' 22                                                   ',
        ' 21          ***                                      ',
        ' 20             *****                                 ',
        ' 19                  ***                              ',
        ' 18                    *                              ',
        ' 17           ***       *                             ',
        ' 16              *****  *                             ',
        ' 15                   ***                             ',
        ' 14                      *                            ',
        ' 13                      *                            ',
        ' 12                 ***  *                            ',
        ' 11                    ****                           ',
        ' 10                       *                           ',
        '  9                       *                           ',
        '  8                       *                           ',
        '  7                        *                          ',
        '  6                        *                          ',
        '  5                        *                          ',
        '  4                         *                         ',
        '  3                         *                         ',
        '  2                         *                         ',
        '  1                          *                        ',
        '  0                          *                        ',
        ' -1                                                   ',
        ' -2                                                   ',
        ' -3                                                   ',
        ' -4                                                   ',
        ' -5                                                   ',
        ' -6                                                   ',
        ' -7                                                   ',
        ' -8                                                   ',
        ' -9                                                   ',
        '-10                                                   ',
        '-11                                                   ',
        '-12                                                   ',
        '-13                                                   ',
        '-14                                                   ',
        '-15                                                   ',
        '-16                                                   ',
        '-17                                                   ',
        '-18                                                   ',
        '-19                                                   ',
        '-20                                                   ',
        '-21                                                   ',
        '-22                                                   ',
        '-23                                                   ',
        '-24                                                   ',
        '-25                                                   ',
        ]
    return image_rows

  def testWindBarbs(self):
    s = TransformStack()
    s.Scale(20, 20)

    bit_matrix = BitMatrix('rose')

    wind_dir = 342.0  # degrees
    wind_speed = 28.0  # Knots

    # WindBarbGenerator
    windbarb.RasterWindBarbGenerator(
        bit_matrix, s, Polar(wind_speed, wind_dir), True)
    sio = StringIO.StringIO()
    bit_matrix.DrawAscii(sio)
    expect_image_rows = self.GetExpectAsciiWindBarb()
    for r0, r1 in zip(expect_image_rows, sio.getvalue().split('\n')):
      self.assertEqual(r0, r1)

    traversal = windbarb.TraversalArtifact()
    windbarb.ExploreLine(bit_matrix, 0, Point(0, 0), traversal)

    best_fits = traversal.FindBestFittingLinesForEndPoints('demo')

    # We expect to find 4 lines, the shaft of length 20
    # the 2 longer barbs of near length 10,
    # and the shortest barb near length 5
    # We test here in an empirical traversal order
    expect_int_polars = [
        Polar(5, 101),
        Polar(10, 101),
        Polar(10, 101),
        Polar(20, wind_dir)]

    for expect_polar, result_best_fit, in zip(expect_int_polars, best_fits):
      self.assertEqual(expect_polar, result_best_fit.GetBestPolar())


def main():
  unittest.main()


if __name__ == '__main__':
  main()
